package com.blogspot.wanyancan.client.image;

import com.blogspot.wanyancan.client.FormConstants;
import com.blogspot.wanyancan.client.image.ImagePanel.ImageProvider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.core.java.lang.boolean_Array_CustomFieldSerializer;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CellPanel extends Composite {
	final String[] radioSet = new String[]{"no.1", "no.2", "no.3","no.4","no.5","no.6","no.7"};
	
	final FormPanel form = new FormPanel();

	final DockPanel mpanel = new DockPanel();

	final Grid inner = new Grid(3, 3);

	final Label lbName = new Label(), lbDesc = new Label();

	final TextBox tbName = new TextBox(), tbDesc = new TextBox();
	final FlowPanel imgpanel = new FlowPanel();
	final StarProPanel starpanel;

	DynaTableDataProvider provider;
	DynaTableWidget parent;
	
	public CellPanel(DynaTableDataProvider provider, DynaTableWidget parent) {
		// service provider
		this.provider = provider;
		this.parent = parent; 
		FormConstants fmConstants = (FormConstants)GWT.create(FormConstants.class);
		
		initWidget(form);
		form.setAction(GWT.getModuleBaseURL() + "/imagecell");
		// form.setEncoding(FormPanel.ENCODING_);
		form.setMethod(FormPanel.METHOD_POST);
		form.setWidget(mpanel);

		// name & desc
		mpanel.add(inner, DockPanel.SOUTH);
		lbName.setText(fmConstants.nameString());
		lbDesc.setText(fmConstants.descString());
		inner.setWidget(0, 0, lbName);
		inner.setWidget(0, 1, tbName);
		tbName.setEnabled(false);
		inner.setWidget(2, 0, lbDesc);
		inner.setWidget(2, 1, tbDesc);
		
		// radio parts, select props
		starpanel = new StarProPanel();
		inner.setWidget(0, 2, starpanel);
		
		// image lists panel
		Image img = new Image("star_off_sm_2.gif");
		imgpanel.add(img);
		inner.setWidget(1, 0, imgpanel);
		imgpanel.setWidth("100%");
	}
	class StarProPanel extends Composite{
		final HorizontalPanel propPanel = new HorizontalPanel();
		Image[] stars;
		public StarProPanel() {
			stars = new Image[radioSet.length];
			for(int i = 0; i<radioSet.length; i++){
				stars[i] = new Image("star_off_sm_2.gif");
				stars[i].addClickListener(new ClickListener(){
					public void onClick(Widget sender) {
						// TODO: need to be more efficient, by server side
						final Image img = (Image)sender;
						boolean isGoingOn = true;
						if(img.getUrl().indexOf("on")!=-1){
							isGoingOn = false;
							//img.setUrl("star_off_sm_2.gif");
						}//else img.setUrl("star_on_sm_2.gif");
						AsyncCallback callback = new AsyncCallback(){
							public void onFailure(Throwable caught) {
								String msg = "Failed to update data to server\n";
								if (caught != null) {
									msg += ": " + caught.getMessage();
								}
								parent.setStatusText(msg);
							}
							public void onSuccess(Object result) {
								parent.setStatusText("properties changed");
								if(img.getUrl().indexOf("on")!=-1)
									img.setUrl("star_off_sm_2.gif");
								else img.setUrl("star_on_sm_2.gif");
							};
						};
						String newprop = "";
						for(int i = 0; i<stars.length; i++){
							if(stars[i]!=img &&  stars[i].getUrl().indexOf("on")!=-1|| (isGoingOn && stars[i]==img)) 
								newprop+= (i+1)+",";
						}
						parent.setStatusText("Summitting...");
						GWT.log("wanna to summit prop to "+newprop, null);
						((ImageProvider)provider).imgService.updateImgClass(tbName.getText(), newprop, callback);
					}
				});
				propPanel.add(stars[i]);
				propPanel.add(new Label(radioSet[i]));
			}
			initWidget(propPanel);
		}
		/**
		 * props is integer list, 1-based in database
		 * @param props, comma seprated num list
		 */
		public void updateStars(String props){
			String[] proplist = props.split(",");
			for(int i = 0; i<stars.length; i++){
				boolean found = false;
				for(int j = 0; j<proplist.length; j++){
					if(proplist[j].equals(String.valueOf(i+1))){  // prop is 1-based
						stars[i].setUrl("star_on_sm_2.gif");
						found = true;
						break;
					}
				}
				if(!found) stars[i].setUrl("star_off_sm_2.gif");
			}
		}
	}
	public void UpdateCell(ImageRec imgrec){
		tbName.setText(imgrec.getName());
		tbDesc.setText(imgrec.getDescription());
		imgpanel.add(new Label(imgrec.getProp()));
		starpanel.updateStars(imgrec.getProp());
	}
}
